<template>
  <div class="news-list">
    <div class="filter-section">
      <el-tabs v-model="activeCategory" @tab-click="handleCategoryChange">
        <el-tab-pane label="全部" name="all" />
        <el-tab-pane label="校园新闻" name="campus" />
        <el-tab-pane label="学术动态" name="academic" />
        <el-tab-pane label="通知公告" name="notice" />
      </el-tabs>
    </div>

    <div class="news-grid">
      <el-row :gutter="20">
        <el-col v-for="news in newsList" :key="news.id" :xs="24" :sm="12" :md="8" :lg="6">
          <router-link :to="{ name: 'NewsDetail', params: { id: news.id }}" class="news-card-link">
            <el-card class="news-card" :body-style="{ padding: '0px' }">
              <div class="news-image">
                <el-image 
                  :src="news.coverImage || '/default-news.jpg'" 
                  fit="cover"
                  lazy
                >
                  <template #error>
                    <div class="image-placeholder">
                      <el-icon><Picture /></el-icon>
                    </div>
                  </template>
                </el-image>
              </div>
              <div class="news-content">
                <h3 class="news-title">{{ news.title }}</h3>
                <p class="news-summary">{{ news.summary }}</p>
                <div class="news-meta">
                  <div class="news-tags">
                    <el-tag v-for="tag in news.tags" :key="tag" size="small" effect="plain">
                      {{ tag }}
                    </el-tag>
                  </div>
                  <span class="news-date">{{ formatDate(news.publishTime) }}</span>
                </div>
              </div>
            </el-card>
          </router-link>
        </el-col>
      </el-row>
    </div>

    <div class="pagination">
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :page-sizes="[12, 24, 36, 48]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Picture } from '@element-plus/icons-vue'
import { listNews } from '@/api/news'
import { formatDate } from '@/utils/date'

const activeCategory = ref('all')
const newsList = ref([])

const pagination = reactive({
  page: 1,
  size: 12,
  total: 0
})

const getNewsList = async () => {
  try {
    const res = await listNews({
      ...pagination,
      category: activeCategory.value === 'all' ? '' : activeCategory.value
    })
    if (res.code === 200) {
      newsList.value = res.data.list
      pagination.total = res.data.total
    }
  } catch (error) {
    console.error('获取新闻列表失败:', error)
  }
}

const handleCategoryChange = () => {
  pagination.page = 1
  getNewsList()
}

const handleSizeChange = (val) => {
  pagination.size = val
  pagination.page = 1
  getNewsList()
}

const handleCurrentChange = (val) => {
  pagination.page = val
  getNewsList()
}

onMounted(() => {
  getNewsList()
})
</script>

<style scoped>
.news-list {
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
}

.filter-section {
  margin-bottom: 24px;
}

.news-grid {
  margin-bottom: 24px;
}

.news-card-link {
  text-decoration: none;
  color: inherit;
  display: block;
  margin-bottom: 20px;
}

.news-card {
  transition: all 0.3s;
  height: 100%;
}

.news-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.news-image {
  height: 200px;
  overflow: hidden;
}

.news-image .el-image {
  width: 100%;
  height: 100%;
}

.image-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  color: #909399;
  font-size: 24px;
}

.news-content {
  padding: 16px;
}

.news-title {
  margin: 0 0 12px;
  font-size: 18px;
  font-weight: 600;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.news-summary {
  color: #666;
  font-size: 14px;
  line-height: 1.5;
  margin: 0 0 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.news-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #999;
}

.news-tags {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 32px;
}

@media (max-width: 768px) {
  .news-list {
    padding: 12px;
  }

  .news-image {
    height: 160px;
  }

  .news-title {
    font-size: 16px;
  }
}
</style> 